import { String } from "../utils/string";
import { UserInfo } from "./user-info";

export class LocalUser {
  // 尝试获取用户信息
  public static tryGetUserInfo() {
    const userInfoJson = localStorage.getItem("flying-eye-user-info");
    if (String.isNullOrWhitespace(userInfoJson)) {
      return null;
    }
    const userInfo = JSON.parse(userInfoJson!) as UserInfo;
    Object.setPrototypeOf(userInfo, UserInfo.prototype);
    return userInfo;
  }

  // 设置用户信息
  public static setUserInfo(userInfo: UserInfo) {
    localStorage.setItem("flying-eye-user-info", JSON.stringify(userInfo));
  }

  // 清除用户信息
  public static cleanUserInfo() {
    localStorage.removeItem("flying-eye-user-info");
  }
}
